export function formatDate (dateString) {
	var date = new Date(dateString);

	return [date.getFullYear(), date.getMonth(), date.getDay()].join('.');
}

var pad = function (num, size) {
	var s = "0000" + num;

	return s.substr(s.length - size);
};

export function formatTime (dateString) {
	var date = new Date(dateString);
	var hh = date.getHours();
	var mm = date.getMinutes();
	var ss = date.getSeconds();
	var sss = date.getMilliseconds();

	return pad(hh, 2) + ':' + pad(mm, 2) + ':' + pad(ss, 2) + '.' + pad(sss, 3);
}

var templateParser = function (string, scope) {
	return string.replace(/\$\{([\s\S]*?)\}/g, function ($, code) {
		var scoped = code.replace(/(["'\.\w\$]+)/g, function (match) {
			return (/["'\d]/).test(match[0]) ? match : 'scope.' + match;
		});
		try {
			return new Function('scope', 'return ' + scoped)(scope);
		} catch (e) { return '错误：\n' + e; }
	});
};
	
var template = {
	0x00: "进入 ${processName} 过程",
	0x01: "退出过程",
	0x02: "${isPassed?'成功':'异常'} 退出本次循环",

	0x11: "等待 ${delay} 毫秒",
	0x12: "${action} 动作执行${isPassed?'成功':'失败'}，${param&&param.value?'参数：'+param.value+',':''} 对象:${object}",
	0x13: "断言 ${isPassed?'成功':'失败'}",
	0x14: "跳转到 ${url}",
	0x15: "刷新",
	"-1": "退出第 ${timeNumber} 次测试",
	"100": "发生错误"
};

export function formatLog (input) {
	return templateParser(template[input.type], input);
}
